package com.xyw.code.platform.webapps.rest;

import com.xyw.code.common.core.constant.IsDelEnum;
import com.xyw.code.common.core.entity.vo.Result;
import com.xyw.code.common.core.entity.vo.SecurityUser;
import com.xyw.code.common.core.util.UserContextHolder;
import com.xyw.code.platform.common.entity.dto.MenuDTO;
import com.xyw.code.platform.common.entity.po.SysMenu;
import com.xyw.code.platform.common.entity.util.XUtil;
import com.xyw.code.platform.common.entity.vo.MenuVo;
import com.xyw.code.platform.webapps.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author xyw
 * @since 2020-02-07
 */
@RestController
@RequestMapping("/sysMenu")
@Api(value="菜单模块")
public class SysMenuController {

    @Autowired
    private ISysMenuService menuService;

    @GetMapping("/all")
    @ApiOperation(value = "获取所有可用的菜单",notes = "获取所有可用的菜单")
    public Result<List<SysMenu>> allResource(){
        List<SysMenu> sysMenus = menuService.lambdaQuery().eq(SysMenu::getDelFlag, IsDelEnum.USE.getKey()).list();
        return Result.success(sysMenus.stream().
                filter(sysMenu -> StringUtils.isNotBlank(sysMenu.getPerms())).collect(Collectors.toList()));
    }

    @RequestMapping("/user/{username}")
    @ApiOperation(value = "根据用户名获取资源",notes = "根据用户名获取资源")
    public Result<List<SysMenu>> userResource(@PathVariable(value = "username") String username){
        List<SysMenu> sysMenus = menuService.userResource(username);
        return Result.success(sysMenus.stream().
                filter(sysMenu -> StringUtils.isNotBlank(sysMenu.getPerms())).collect(Collectors.toList()));
    }

    /**
     * 获取路由
     *
     * @return
     */
    @GetMapping("/getRouters")
    @ApiOperation(value = "获取路由",notes = "获取路由")
    public Result<List<MenuVo>> getRouters() {
        SecurityUser securityUser = UserContextHolder.getInstance().getContext();
        //根据用户得到用户所拥有的角色 根据角色拥有该角色所有的菜单
        return Result.success(XUtil.buildVueMenu(menuService.selectMenuTree(securityUser.getUserId())));
    }


    /**
     * 获取所有菜单
     *
     * @return
     */
    @GetMapping("/getMenus")
    @ApiOperation(value = "获取所有菜单",notes = "获取所有菜单")
    public Result getMenus() {
        return Result.success(menuService.selectMenuTree(null));
    }

    /**
     * 获取菜单树
     *
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "获取菜单树",notes = "获取菜单树")
    public Result<SysMenu> queryMenuTreeList(@RequestParam(required = false) String name) {
        return Result.success(menuService.getMenuTreeList(name));
    }

    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增菜单",notes = "新增菜单")
    public Result add(@RequestBody SysMenu menu) {
        return Result.success(menuService.save(menu));
    }

    /**
     * 修改菜单
     *
     * @param menuDto
     * @return
     */
    @ApiOperation(value = "更新菜单",notes = "更新菜单")
    @PutMapping("/update")
    public Result update(@RequestBody MenuDTO menuDto) {
        return Result.success(menuService.updateMenuById(menuDto));
    }


    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除菜单",notes = "删除菜单")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(menuService.deleteMenu(id));
    }

}

